package com.wm.wmcrm.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.wmcrm.common.util.PageUtils;
import com.wm.wmcrm.sys.entity.SysRole;
import com.wm.wmcrm.sys.mapper.SysRoleMapper;
import com.wm.wmcrm.sys.model.SysRoleQueryDTO;
import com.wm.wmcrm.sys.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author wm
 * @since 2023-02-11
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Resource
    SysRoleMapper sysRoleMapper;

    @Override
    public PageUtils queryPage(SysRoleQueryDTO queryDTO) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<SysRole>().like(
                StringUtils.isNotEmpty(queryDTO.getRoleName()),"role_name",queryDTO.getRoleName()
        );
        Page<SysRole> page = this.page(queryDTO.page(),wrapper);
        return new PageUtils(page);
    }

    @Override
    public boolean saveRole(SysRole role) {
        return this.save(role);
    }

    @Override
    public boolean updateOne(SysRole role) {
        return this.updateById(role);
    }

    @Override
    public void deleteBatch(Long[] roleIds) {

    }

    @Override
    public int checkNameExist(String roleName) {
        if(StringUtils.isEmpty(roleName)) return -1;
        QueryWrapper<SysRole> QueryWrapper = new QueryWrapper<SysRole>();
        QueryWrapper.eq("role_name",roleName);
        int count = this.count(QueryWrapper);
        return count;
    }

    @Override
    public int deleteBatch(List<String> idList) {
        return this.baseMapper.deleteBatchIds(idList);
    }

    @Override
    public boolean deleteOne(String roleId) {
        return this.removeById(roleId);
    }

    @Override
    public List<SysRole> queryByUserId(Long userId) {
        return sysRoleMapper.queryByUserId(userId);
    }

}
